package auctionhausen

import org.springframework.dao.DataIntegrityViolationException

class CustomerController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def customerService
	
	def authenticate() {
		def user = Customer.findByEmailAndPassword(params.email, params.password)
		if(user){
			session.user = user
			flash.message += "<pHello ${user.email}!</p"
			redirect(controller:"Listing", action:"list")
		}else{
			flash.message += "<pSorry, ${params.email} - invalid login. Please try again.</p"
			redirect(action:"login")
		}
	}

	def login() {
		if (request.post) {
			// Process the login request only for a POST request
			authenticate()
			return
		}
		// Otherwise, display the login page "login.gsp"
	}

	def logout() {
		flash.message = "<pYou've been successfully logged out, ${session.user.email}.</p"
		session.user = null
		redirect(controller:"Listing", action:"list")
	}

	def register() {
		if (request.post) {
			// Process the registration request only for a POST request
			def customerInstance = new Customer(params)
			if (!customerInstance.validate() || !customerInstance.save(flush: true)) {
				render(view: "register", model: [customerInstance: customerInstance])
				return
			}

			flash.message = "<pRegistration successful!</p";

			// Log user in and send to main page.
			authenticate()
		}
		// Otherwise, display the registration page "register.gsp"
	}
	def index() {
		redirect(action: "list", params: params)
	}

	def list() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[customerInstanceList: Customer.list(params), customerInstanceTotal: Customer.count()]
	}

	def create() {
		[customerInstance: new Customer(params)]
	}

	def save() {
		def customerInstance = customerService.createCustomer(params)
		if (customerInstance.hasErrors()) {
			render(view: "create", model: [customerInstance: customerInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'customer.label', default: 'Customer'),
			customerInstance.id
		])
		redirect(action: "show", id: customerInstance.id)
	}

	def show() {
		def customerInstance = Customer.get(params.id)
		if (!customerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[customerInstance: customerInstance]
	}

	def edit() {
		def customerInstance = Customer.get(params.id)
		if (!customerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[customerInstance: customerInstance]
	}

	def update() {
		def customerInstance = Customer.get(params.id)
		if (!customerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		if (params.version) {
			def version = params.version.toLong()
			if (customerInstance.version > version) {
				customerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[
							message(code: 'customer.label', default: 'Customer')]
						as Object[],
						"Another user has updated this Customer while you were editing")
				render(view: "edit", model: [customerInstance: customerInstance])
				return
			}
		}

		customerInstance.properties = params

		if (!customerInstance.save(flush: true)) {
			render(view: "edit", model: [customerInstance: customerInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'customer.label', default: 'Customer'),
			customerInstance.id
		])
		redirect(action: "show", id: customerInstance.id)
	}

	def delete() {
		def customerInstance = Customer.get(params.id)
		if (!customerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		if (customerInstance?.listings?.size() > 0 || customerInstance?.bids?.size() > 0) {
			flash.message = message(code: 'customer.not.deleted.message',
					args: [
						message(code: 'customer.label', default: 'Customer'),
						customerInstance.toString(),
						customerInstance?.listings?.size() ?: 0,
						customerInstance?.bids?.size() ?: 0
					])
			redirect(action: "show", id: params.id)
			return
		}
		else {
			try {
				customerInstance.delete(flush: true)
				flash.message = message(code: 'default.deleted.message', args: [
					message(code: 'customer.label', default: 'Customer'),
					params.id
				])
				redirect(action: "list")
			}
			catch (DataIntegrityViolationException e) {
				flash.message = message(code: 'default.not.deleted.message', args: [
					message(code: 'customer.label', default: 'Customer'),
					params.id
				])
				redirect(action: "show", id: params.id)
			}
		}
	}
}